package cn.stylefeng.guns.modular.api.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.api.entity.Farm;
import cn.stylefeng.guns.modular.api.mapper.FarmMapper;
import cn.stylefeng.guns.modular.api.mapper.PoodMapper;
import cn.stylefeng.guns.modular.api.model.params.FarmParam;
import cn.stylefeng.guns.modular.api.model.params.PlanParam;
import cn.stylefeng.guns.modular.api.model.params.PoodParam;
import cn.stylefeng.guns.modular.api.model.params.ProductParam;
import cn.stylefeng.guns.modular.api.model.result.FarmResult;
import cn.stylefeng.guns.modular.api.model.result.PlanResult;
import cn.stylefeng.guns.modular.api.model.result.ProductResult;
import  cn.stylefeng.guns.modular.api.service.FarmService;
import cn.stylefeng.guns.modular.api.service.PlanService;
import cn.stylefeng.guns.modular.api.service.PoodService;
import cn.stylefeng.guns.modular.api.service.ProductService;
import cn.stylefeng.guns.modular.api.vo.FarmStatVo;
import cn.stylefeng.guns.modular.api.vo.PlanMapObectVo;
import cn.stylefeng.guns.modular.api.vo.StatTypeEnum;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;
import sun.security.krb5.internal.PAData;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 * 养殖场 服务实现类
 * </p>
 *
 * @author wusonghe
 * @since 2019-08-31
 */
@Service
public class FarmServiceImpl extends ServiceImpl<FarmMapper, Farm> implements FarmService {

    @Autowired
    private PlanService planService;

    @Autowired
    private ProductService productService;

    @Autowired
    private PoodService poodService;

    @Override
    @Transactional
    public void add(FarmParam param){

        //计划保存
        Integer farmId = Math.abs(new Random().nextInt());
        param.setFarmId(farmId);
        Farm entity = getEntity(param);
        this.save(entity);
        if (param.getNumber()!=null && param.getPoodNumber()!=null){
            for (Integer i = 1; i < param.getPoodNumber()+1; i++) {
                PoodParam poodParam = new PoodParam();
                poodParam.setFarmId(farmId);
                poodParam.setTitle(param.getNumber() + i.toString());
                poodService.add(poodParam);
            }
        }
    }

    @Override
    @Transactional
    public void delete(FarmParam param){
        this.removeById(getKey(param));

        PoodParam poodParam = new PoodParam();
        poodParam.setFarmId(param.getFarmId());
        poodService.deleteParam(poodParam);
    }

    @Override
    @Transactional
    public void update(FarmParam param){
        Farm oldEntity = getOldEntity(param);
        Farm newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);

        if (param.getNumber()!=null && param.getPoodNumber()!=null){

            Integer originNumber = oldEntity.getPoodNumber();
            Integer nowNumber = param.getPoodNumber() - originNumber;
            if (nowNumber > 0) {
                for (Integer i = 1; i < nowNumber+1; i++) {
                    PoodParam poodParam = new PoodParam();
                    poodParam.setFarmId(newEntity.getFarmId());
                    Integer num = i + originNumber;
                    poodParam.setTitle(param.getNumber() + num.toString());
                    poodService.add(poodParam);
                }
            }

        }
    }

    @Override
    public FarmResult findBySpec(FarmParam param){
        return null;
    }

    @Override
    public List<FarmResult> findListBySpec(FarmParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(FarmParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext,param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(FarmParam param){
        return param.getFarmId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Farm getOldEntity(FarmParam param) {
        return this.getById(getKey(param));
    }

    private Farm getEntity(FarmParam param) {
        Farm entity = new Farm();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }



    /**
     * 统计信息
     * @param farmId
     * @param dateTime
     * @return
     */
    public FarmStatVo statistics(Integer farmId,
                                    String dateTime) {

        FarmStatVo farmStatVo = new FarmStatVo();
        farmStatVo.setStatType(StatTypeEnum.PRODUCT_KG.getCode());

        Map<String,Object> farmMap = new HashMap<String,Object>();
        //把计划的产量加起来
        Double planWeights = 0.0;
        PlanParam planParam = new PlanParam();
        planParam.setFarmId(farmId);
        List<PlanResult> planResults = planService.findListBySpec(planParam);
        for (PlanResult planResult:planResults) {
            if (planResult.getPlanProductList()!=null) {
                Map maps = (Map) JSON.parse(planResult.getPlanProductList());
                List list = (List)maps.get("data");
                for (Object o:list) {
                    PlanMapObectVo planMapObectVo = JSON.parseObject(JSON.toJSONString(o), PlanMapObectVo.class);
                    planWeights += Double.parseDouble(planMapObectVo.getNumber()) *  Double.parseDouble(planMapObectVo.getWeight());
                }
            }
        }
        farmMap.put("plan",planWeights);


        //把所有产品的产量加起来
        Double prductWeights = 0.0;
        ProductParam productParam = new ProductParam();
        planParam.setFarmId(farmId);
        List<ProductResult> productResults = productService.findListBySpec(productParam);
        for (ProductResult productResult:productResults) {
            if (productResult.getWeight()!=null) {
                prductWeights += productResult.getNumber().doubleValue() * productResult.getWeight();

            }
        }
        farmMap.put("product",prductWeights);

        farmStatVo.setDatas(farmMap);
        return farmStatVo;
    }

}
